﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL.Admin;

public partial class Admin_addAdminBigFunction : System.Web.UI.Page
{
    private static string AID = "";
    private AspNetPagerInfo info = new AspNetPagerInfo();
    private AspNetPager Pager = new AspNetPager();
    //用于刷新的方法
    private void bindGrid()
    {
        try
        {

            //给存储过程传参数
            info.TableName = "AdminBigFunction";
            info.PageSize = AspNetPager1.PageSize;
            info.PageIndex = AspNetPager1.CurrentPageIndex;
            info.KeyField = "ABFID";
            info.FieldType = "int";
            info.SqlFile = "*";
            info.SqlOrder = "order by ABFID";
            info.SqlWhere = "where 1=1";
            AspNetPager1.RecordCount = Pager.GetRecordCount(info);
            GVAdmin.DataSource = Pager.GetListForDataSet(info);
            GVAdmin.DataBind();

            update.Visible = false;
            lblMessages.Visible = false;
            bntInsert.Visible = false;
        }
        catch (Exception ex)
        {
            ClientScriptManager csm = this.ClientScript;
            csm.RegisterStartupScript(this.GetType(), "admin", "alter('" + ex.Message.ToString().Trim() + "')", true);
        }
    }

    //加载的时候执行
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["admin"].ToString().Trim() != "1")
        {
            Response.Redirect("Login.aspx");
        }
        bntInsert.Visible = false;
        if (!IsPostBack)
        {
            bindGrid();
        }
    }

    //点修改的时候
    protected void GVAdmin_SelectedIndexChanged(object sender, EventArgs e)
    {
        update.Visible = true;
        bntInsert.Visible = false;
        btnUpdate.Visible = true;
        try
        {
            txtPwd.Text = GVAdmin.SelectedRow.Cells[1].Text.ToString().Trim();
            AID = GVAdmin.SelectedRow.Cells[0].Text.ToString().Trim();
        }
        catch (Exception ex)
        {
            lblMessages.Visible = true;
            lblMessages.Text = ex.Message.ToString().Trim();
        }
    }
    protected void GVAdmin_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            lblMessages.Visible = false;
            if (AdminServices.SP_delete("AdminBigFunction", "ABFID=" + GVAdmin.Rows[e.RowIndex].Cells[0].Text.ToString().Trim()))
            {
                lblMessages.Visible = true;
                lblMessages.Text = "删除成功";
                bindGrid();
            }
            else
            {
                lblMessages.Visible = true;
                lblMessages.Text = "删除失败";
            }
        }
        catch (Exception ex)
        {
            lblMessages.Visible = true;
            lblMessages.Text = ex.Message.ToString().Trim();
        }
    }
    //修改
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            string strSql = "update AdminBigFunction set ABFName='" + txtPwd.Text.ToString().Trim() + "'  where  ABFID=" + Convert.ToInt32(AID.ToString().Trim());
            if (AdminServices.SP_sqlStr_Update_Delete_insert(strSql))
            {
                lblMessages.Visible = true;
                lblMessages.Text = "修改成功";
                bindGrid();
                update.Visible = false;
            }
            else
            {
                lblMessages.Visible = true;
                lblMessages.Text = "修改失败";
            }
        }
        catch (Exception ex)
        {
            lblMessages.Visible = true;
            lblMessages.Text = ex.Message.ToString().Trim();
        }
    }

    //添加新用户时
    protected void LinkButton1_Click(object sender, EventArgs e)
    {

        try
        {
            txtPwd.Text = "";
            update.Visible = true;
            btnUpdate.Visible = false;
            bntInsert.Visible = true;
        }
        catch (Exception ex)
        {
            lblMessages.Visible = true;
            lblMessages.Text = ex.Message.ToString().Trim();
        }
    }

    //添加
    protected void bntInsert_Click(object sender, EventArgs e)
    {
        try
        {
            string strSql = "insert into  AdminBigFunction(ABFName)values('" + txtPwd.Text.ToString().Trim() + "')";
            if (AdminServices.SP_sqlStr_Update_Delete_insert(strSql))
            {
                lblMessages.Visible = true;
                lblMessages.Text = "添加成功";
                bindGrid();
                update.Visible = false;
            }
            else
            {
                lblMessages.Visible = true;
                lblMessages.Text = "添加失败";
            }
        }
        catch (Exception ex)
        {
            lblMessages.Visible = true;
            lblMessages.Text = ex.Message.ToString().Trim();
        }
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        bindGrid();
    }
}

